package web.controller.user;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.RatingService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.RatingRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/7/6.
 * 对一个商家发起评价
 */
@RestController("userRating")
@RequestMapping(Routes.User.RATING)
public class Rating extends Base {
    @Autowired
    private RatingService ratingService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse rate(@RequestBody RatingRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return fail(result.getMessage());

        }
        long user = getUserIdFromAuthHeader();
        /* applier is the Rater character, @see employer.rate() */
        return ratingService.rate2Employer(user, request.getRatee(), request.getRecruitment(), request.getRate(), request.getComment());
    }

    @Protection
    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse mine() {

        long user = getUserIdFromAuthHeader();

        return ratingService.getMyRateFromEmployer(user);
    }
}
